package ewa.big.controller;

import at.ac.big.tuwien.ewa.picasa.Avatar;
import ewa.big.entities.Player;
import ewa.big.entities.RegisteredPlayerPool;
import ewa.big.utilities.Utility;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.validator.ValidatorException;

/**
 * <b>Web Engineering - UE3</b>
 * <p>
 *
 * This class represents the register control of the "Mensch aergere Dich nicht" game.
 * 
 * @version 1.00 (01.05.2012)
 */
@ManagedBean(name = "rc")
@ViewScoped
public class RegisterControl implements Serializable {
    private static final long serialVersionUID = 1L;

    @ManagedProperty(value = "#{player}")
    private Player newPlayer;
    @ManagedProperty(value = "false")
    private boolean displayPrivateData;
    @ManagedProperty(value = "#{rpp}")
    private RegisteredPlayerPool rpp;
    @ManagedProperty(value = "#{false}")
    private boolean registrationSuccessful;

    /** C'tor  Creates a new instance of RegisterControl */
    public RegisterControl() {
    } // end C'tor

    /**
     * The register method.
     * 
     * @return the forward uri string.
     */
    public String register() {
      boolean stay = false;
      FacesContext ctx = FacesContext.getCurrentInstance();
      
      boolean success;
      if(newPlayer.getUsername() != null && !newPlayer.getUsername().equals("")) {
        success = getRpp().addPlayer(newPlayer);
        
        if(success == true) {
          registrationSuccessful = true;
          if(!stay)  return "index";
        } else {
          String i18ntext = Utility.getResourceText(ctx, "msg", "userexists");
          FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN, i18ntext, null);
//          throw new ValidatorException(msg);
          ctx.addMessage(null, msg);
        }
      } else {
        String i18ntext = Utility.getResourceText(ctx, "msg", "registrationfailed");
        FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_WARN, i18ntext, null);
        ctx.addMessage(null, msg);
      }

      return "register";
    } // end register

    /**
     * Checks if the display checkbox changed.
     * 
     * @param e    the value change event.
     */
    public void displayChanged(ValueChangeEvent e) {
        Boolean show = (Boolean) e.getNewValue();
        if (show != null) {
            displayPrivateData = show;
        }

        FacesContext.getCurrentInstance().renderResponse();
    } // end displayChanged

    /**
     * Validation of the birthday.
     * 
     * @param ctx          the context
     * @param component    the ui component
     * @param value        the value to be validated
     * @throws ValidatorException 
     */
    public void validateBirthday(FacesContext ctx, UIComponent component, Object value) throws ValidatorException {
        String bd = (String) value;

        DateFormat format = new SimpleDateFormat("dd.MM.yyyy");
        format.setLenient(false);

        try {
            format.parse(bd);
        } catch (ParseException e) {
            String i18ntext = Utility.getResourceText(ctx, "msg", "birthdateformat");

            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, i18ntext, null);

            throw new ValidatorException(msg);
        }
    } // end validateBirthday

    /**
     * Get the new player.
     * 
     * @return the new player
     */
    public Player getNewPlayer() {
        return newPlayer;
    } // end getNewPlayer

    /**
     * Set the new player.
     * 
     * @param newplayer     the new player to set
     */
    public void setNewPlayer(Player newPlayer) {
        this.newPlayer = newPlayer;
    } // end setNewPlayer

    /**
     * Checks display private data flag.
     * 
     * @return the display private data
     */
    public boolean isDisplayPrivateData() {
        return displayPrivateData;
    } // end isDisplayPrivateData

    /**
     * Set the display private data flag.
     * 
     * @param displayPrivateData    the display private data to set
     */
    public void setDisplayPrivateData(boolean displayPrivateData) {
        this.displayPrivateData = displayPrivateData;
    } // end setDisplayPrivateData

    /**
     * Get the registered player pool.
     * 
     * @return the rpp
     */
    public RegisteredPlayerPool getRpp() {
        return rpp;
    } // end getRpp

    /**
     * Set the registered player pool.
     * 
     * @param rpp    the rpp to set
     */
    public void setRpp(RegisteredPlayerPool rpp) {
        this.rpp = rpp;
    } // end setRpp

    /**
     * Checks if the registration was successful.
     * 
     * @return the registration successful
     */
    public boolean isRegistrationSuccessful() {
        return registrationSuccessful;
    } // end isRegistrationSuccessful

    /**
     * Set the registration successful.
     * 
     * @param registrationSuccessful    the registration successful to set
     */
    public void setRegistrationSuccessful(boolean registrationSuccessful) {
        this.registrationSuccessful = registrationSuccessful;
    } // end setRegistrationSuccessful
    
} // end of class RegisterControl
